from flask import abort, jsonify
from flask import current_app
from flask import g
from flask import render_template
from flask import request

from info import constants
from info.models import News
from info.modules.news import news_blu
from info.utils.common import user_login_data
from info.utils.response_code import RET


@news_blu.route('/news_collect',methods=["POST"])
@user_login_data
def collect_news():
    """
    收藏新闻
    １.接受参数
    ２.判断参数
    3.查询新闻,并判断新闻是否存在
    :return:
    """

    user = g.user
    if not user:
        return jsonify(errno=RET.SERVERERR, errmsg="用户未登陆")

    #１.接受参数
    news_id = request.json.get("news_id")
    action = request.json.get("action")

    # ２.判断参数
    if not all ([news_id,action]):
        return jsonify(errno=RET.PARAMERR,errmsg="参数错误")

    if action not in ["collect","cancel_collect"]:
        return jsonify(errno=RET.PARAMERR, errmsg="参数错误")

    try:
        news_id = int(news_id)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.PARAMERR, errmsg="参数错误")

    # 3.查询新闻,并判断新闻是否存在
    try:
        news = News.query.get(news_id)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg="数据查询错误")

    if not news:
        return jsonify(errno=RET.NODATA, errmsg="未查询到新闻数据")

    # 4.收藏以及取消收藏
    if action == "cancel_collect":
        # 取消收藏
        if news in user.collection_news:
            user.collection_news.remove(news)

    else:
        # 收藏
        if news not in user.collection_news:
            # 添加到用户的新闻收藏列表
            user.collection_news.append(news)

        return jsonify(errno=RET.OK, errmsg="操作成功")

@news_blu.route('/<int:news_id>')
@user_login_data
def news_detali(news_id):
    """

    :param news_id:
    :return:
    """
    # 查询用户登陆信息
    user = g.user



    # 右侧的新闻排行的逻辑
    news_list=[]
    try:
        news_list = News.query.order_by(News.clicks.desc()).limit(constants.CLICK_RANK_MAX_NEWS)
    except Exception as e:
        current_app.logger.error(e)

    #  定义一个空的字典列表,里面装的就是字典
    news_dict_li = []
    # 遍历对象列表,将对象的字典添加到字典列表中
    for news in news_list:
        news_dict_li.append(news.to_basic_dict())

        # 查询新闻数据
        news = None

        try:
            news = News.query.get(news_id)
        except Exception as e:
            current_app.logger.error(e)

        if not news:
            # 报404错误,404错误统一显示页面后续再处理
            abort(404)

        # 更新新闻的点击次数
        news.clicks += 1

        is_collected = False

        # if 用户已的登陆
        #     判断用户是否收藏当前新闻,如果收藏
        #         is_collected　= True

        if user:
            # 判断用户是否收藏当前新闻,如果收藏
            # collection_news 后面可以不用加all,因为sqlalchemy会在使用的时候取自动加载
            if news in user.collection_news:
                is_collected = True

    data = {
        "user": user.to_dict() if user else None,
        "news_dict_li": news_dict_li,
        "news":news.to_dict(),
        "is_collected":is_collected

    }

    return render_template('news/detail.html',data = data)